from fastapi import APIRouter, HTTPException, Depends
from typing import List
from sqlalchemy.orm import Session
from schema import Remote_Sensing_Image_schema
from controller import Remote_Sensing_Image_controller
from database.db_init import get_db


router = APIRouter()


@router.post("/Remote_Sensing_Image", response_model=Remote_Sensing_Image_schema.Remote_sensing_image, tags=["Remote_Sensing_Image"])
async def create_Remote_Sensing_Image(Remote_Sensing_Image: Remote_Sensing_Image_schema.Remote_sensing_image, db: Session = Depends(get_db)):
    return Remote_Sensing_Image_controller.create_Remote_Sensing_Image(db, Remote_Sensing_Image)


@router.get("/Remote_Sensing_Image/{ID}", response_model=Remote_Sensing_Image_schema.Remote_sensing_image, tags=["Remote_Sensing_Image"])
async def get_Remote_Sensing_Image(ID: int, db: Session = Depends(get_db)):
    db_Remote_Sensing_Image = Remote_Sensing_Image_controller.get_Remote_Sensing_Image(db, ID)
    if db_Remote_Sensing_Image is None:
        raise HTTPException(status_code=404, detail="Remote_Sensing_Image not found")
    return db_Remote_Sensing_Image


@router.get("/Remote_Sensing_Image", response_model=List[Remote_Sensing_Image_schema.Remote_sensing_image], tags=["Remote_Sensing_Image"])
async def get_all_Remote_Sensing_Image(db: Session = Depends(get_db)):
    all_db_Remote_Sensing_Image = Remote_Sensing_Image_controller.get_all_Remote_Sensing_Image(db)
    if all_db_Remote_Sensing_Image is None:
        raise HTTPException(status_code=404, detail="0 Remote_Sensing_Image found, empty table")
    return all_db_Remote_Sensing_Image


@router.put("/Remote_Sensing_Image/{ID}", tags=["Remote_Sensing_Image"])
async def update_Remote_Sensing_Image(ID: int, field_name: str, field_value: str, db: Session = Depends(get_db)):
    try:
        db_Remote_Sensing_Image = Remote_Sensing_Image_controller.update_Remote_Sensing_Image(db, ID, field_name, field_value)
        if db_Remote_Sensing_Image is None:
            raise HTTPException(status_code=404, detail="Remote_Sensing_Image not found")
        return db_Remote_Sensing_Image
    except ValueError as error:
        raise HTTPException(status_code=400, detail=str(error))


@router.delete("/Remote_Sensing_Image/{ID}", tags=["Remote_Sensing_Image"])
async def delete_Remote_Sensing_Image(ID: int, db: Session = Depends(get_db)):
    is_deleted = Remote_Sensing_Image_controller.delete_Remote_Sensing_Image(db, ID)
    if is_deleted is None:
        raise HTTPException(status_code=404, detail=f'Sample {ID} does not exist in database')
    else:
        return {'message': f'Successfully delete sample {ID}'}
